#include "mypushbutton.h"

//myPushButton::myPushButton(QWidget *parent) : QWidget(parent)
//{

//}


myPushButton::myPushButton(QString normalImg,QString pressImg)
{
    this->m_normalImgPath=normalImg;
    this->m_pressImgPath=pressImg;

    QPixmap pix;
    bool ret = pix.load(normalImg);
    if(!ret)
    {
        qDebug()<<"打开资源文件失败";
        return;
    }
    //设置按钮大小
    this->setFixedSize(pix.width(),pix.height());
    //设置按钮样式
    this->setStyleSheet("QPushButton{border:0px}");
    //设置图标
    this->setIcon(pix);
    //设置图标大小
    this->setIconSize(QSize(pix.width(),pix.height()));
}

void myPushButton::jumpup()
{
    QPropertyAnimation* animation = new QPropertyAnimation(this,"geometry");
    animation->setDuration(200);
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEasingCurve(QEasingCurve::OutElastic);
    animation->start();
}

void myPushButton::jumpdown()
{
    QPropertyAnimation* animation = new QPropertyAnimation(this,"geometry");
    animation->setDuration(200);
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()-10,this->width(),this->height()));
    animation->setEasingCurve(QEasingCurve::OutElastic);
    animation->start();
}

void myPushButton::mousePressEvent(QMouseEvent *e)
{
    if(this->m_pressImgPath!="")
    {
        QPixmap pix;
        pix.load(this->m_pressImgPath);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mousePressEvent(e);
}

void myPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(this->m_pressImgPath!="")
    {
        QPixmap pix;
        pix.load(this->m_normalImgPath);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mouseReleaseEvent(e);
}

